package com.ikunikun.controller.admin;

import com.ikunikun.dto.DishDTO;
import com.ikunikun.dto.DishPageQueryDTO;
import com.ikunikun.result.PageResult;
import com.ikunikun.result.Result;
import com.ikunikun.service.DishService;
import com.ikunikun.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {

	@Resource
	private DishService dishService;

	/**
	 * 新增菜品
	 * @param dishDTO
	 * @return
	 */
	@CacheEvict(cacheNames = "dishCache", key = "#dishDTO.categoryId")
	@PostMapping
	@ApiOperation("新增菜品")
	public Result save(@RequestBody DishDTO dishDTO){
		log.info("新增菜品:{}", dishDTO);
		dishService.saveWithFlavor(dishDTO);
		return Result.success("新增菜品成功");
	}

	/**
	 * 菜品分页查询
	 * @param dishPageQueryDTO
	 * @return
	 */
	@ApiOperation("分页查询菜品")
	@GetMapping("/page")
	public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
		log.info("菜品分页查询：{}", dishPageQueryDTO);
		PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
		return Result.success(pageResult);
	}

	@CacheEvict(cacheNames = "dishCache", allEntries = true)
	@ApiOperation("菜品批量删除")
	@DeleteMapping
	public Result delete(@RequestParam List<Long> ids){
		log.info("删除菜品：{}", ids);
		dishService.deleteBatch(ids);
		return Result.success("删除菜品成功");
	}

	/**
	 * 根据id查询菜品
	 * @param id
	 * @return
	 */
	@ApiOperation("根据id查询菜品信息")
	@GetMapping("/{id}")
	public Result<DishVO> getById(@PathVariable Long id){
		DishVO dishVO = dishService.getByIdWithFlavor(id);
		return Result.success(dishVO);
	}

	/**
	 * 修改菜品状态
	 * @return
	 */
	@CacheEvict(cacheNames = "dishCache", allEntries = true)
	@PostMapping("/status/{status}")
	@ApiOperation("修改菜品转态")
	public Result<String> stopOrStart(@PathVariable Integer status, Long id){
		dishService.stopOrStart(status, id);
		return Result.success("修改菜品状态成功");
	}

	/**
	 * 修改菜品
	 * @return
	 */
	@CacheEvict(cacheNames = "dishCache", key = "#dishDTO.categoryId")
	@PutMapping
	@ApiOperation("修改菜品")
	public Result<String> update(@RequestBody DishDTO dishDTO){
		log.info("修改菜品：{}", dishDTO);
		dishService.updateWithFlavor(dishDTO);
		return Result.success("修改菜品成功");
	}

	/**
	 * 根据categoryId查询菜品
	 * @param categoryId
	 * @return
	 */
	@GetMapping("/list")
	public Result<List<DishVO>> list(Long categoryId){
		List<DishVO> dishList = dishService.getByCategoryId(categoryId);
		return Result.success(dishList);
	}
}
